<?php
class ModelNewsCategoryNews extends Model {

	public function addCategory($data) {
		$this->load->model('tool/seo_url');
		$this->db->query("INSERT INTO " . DB_PREFIX . "category_news SET parent_news_id = '" . (int)$data['parent_news_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW(), date_added = NOW()");
		$category_news_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "category_news SET image = '" . $this->db->escape($data['image']) . "' WHERE category_news_id = '" . (int)$category_news_id . "'");
		}

		foreach ($data['category_news_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "category_news_description SET category_news_id = '" . (int)$category_news_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "',seo_name='".$this->seo_name('category_news_description',$this->model_tool_seo_url->khongdau($value['name']))."'");
		}

		$this->cache->delete('category_news');
	}
	public function seo_name($table,$title){
            $query = $this->db->query("SELECT * FROM ".DB_PREFIX.$table." WHERE seo_name LIKE '".$title."'");
            if($query->num_rows > 0){
                $index = $query->num_rows;
                $title = $title.'-'.++$index;
            }
            return $title;
        }
	public function editCategory($category_news_id, $data) {
		$this->load->model('tool/seo_url');
		$this->db->query("UPDATE " . DB_PREFIX . "category_news SET parent_news_id = '" . (int)$data['parent_news_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE category_news_id = '" . (int)$category_news_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "category_news SET image = '" . $this->db->escape($data['image']) . "' WHERE category_news_id = '" . (int)$category_news_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "category_news_description WHERE category_news_id = '" . (int)$category_news_id . "'");

		foreach ($data['category_news_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "category_news_description SET category_news_id = '" . (int)$category_news_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "',seo_name='".$this->seo_name('category_news_description',$this->model_tool_seo_url->khongdau($value['name']))."'");
		}
			
		$this->cache->delete('category_news');
	}
	
	public function deleteCategory($category_news_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "category_news WHERE category_news_id = '" . (int)$category_news_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "category_news_description WHERE category_news_id = '" . (int)$category_news_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_to_category WHERE category_news_id = '" . (int)$category_news_id . "'");
		$query = $this->db->query("SELECT category_news_id FROM " . DB_PREFIX . "category_news WHERE parent_news_id = '" . (int)$category_news_id . "'");

		foreach ($query->rows as $result) {
			$this->deleteCategory($result['category_news_id']);
		}
		
		$this->cache->delete('category_news');
	} 

	public function getCategory($category_news_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category_news WHERE category_news_id = '" . (int)$category_news_id . "'");
		
		return $query->row;
	} 
	
	public function getCategories($parent_news_id) {
		$category_data = $this->cache->get('category_news.' . $this->config->get('config_language_id') . '.' . $parent_news_id);
	
		if (!$category_data) {
			$category_data = array();
		
			$sql = "SELECT * FROM " . DB_PREFIX . "category_news c LEFT JOIN " . DB_PREFIX . "category_news_description cd ON (c.category_news_id = cd.category_news_id) WHERE c.parent_news_id = '" . (int)$parent_news_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC";
			$query = $this->db->query($sql);
			foreach ($query->rows as $result) {
				$category_data[] = array(
					'category_news_id' => $result['category_news_id'],
					'name'        => $this->getpath_news($result['category_news_id'], $this->config->get('config_language_id')),
					'sort_order'  => $result['sort_order']
				);
			
				$category_data = array_merge($category_data, $this->getCategories($result['category_news_id']));
			}	
	
			$this->cache->set('category_news.' . $this->config->get('config_language_id') . '.' . $parent_news_id, $category_data);
		}
		
		return $category_data;
	}
	
	public function getpath_news($category_news_id) {
		$query = $this->db->query("SELECT name, parent_news_id FROM " . DB_PREFIX . "category_news c LEFT JOIN " . DB_PREFIX . "category_news_description cd ON (c.category_news_id = cd.category_news_id) WHERE c.category_news_id = '" . (int)$category_news_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		$category_info = $query->row;
		
		if ($category_info['parent_news_id']) {
			return $this->getpath_news($category_info['parent_news_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $category_info['name'];
		} else {
			return $category_info['name'];
		}
	}
	
	public function getCategoryDescriptions($category_news_id) {
		$category_news_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_news_description WHERE category_news_id = '" . (int)$category_news_id . "'");
		
		foreach ($query->rows as $result) {
			$category_news_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}
		
		return $category_news_description_data;
	}	
		
	public function getTotalCategories() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category_news");
		
		return $query->row['total'];
	}	
		
	public function getTotalCategoriesByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category_news WHERE image_id = '" . (int)$image_id . "'");
		
		return $query->row['total'];
	}
}
?>